.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
ObjectExtension[Cast].cs
Go to the documentation of this file.
1 // <copyright file=mitlicense.md url=http://lsauer.mit-license.org/ >
2 // Lo Sauer, 2013-2016
3 // </copyright>
4 // <summary> A tested, generic, portable, runtime-extensible type converter library </summary
5 // <language> C# > 6.0 </language>
6 // <version> 3.1.0.2 </version>
7 // <author> Lorenz Lo Sauer; people credited in the sources </author>
8 // <project> https://github.com/lsauer/dotnet-portable-type-cast </project>
9 namespace Core.TypeCast
10 {
11  using System;
12  using System.Reflection;
13  using System.Linq;
14  using System.Runtime.CompilerServices;
15 
16  using Core.TypeCast.Base;
17  using Core.Extensions;
18 
20  public static partial class ObjectExtension
21  {
34  [MethodImpl(MethodImplOptions.AggressiveInlining)]
35  public static TOut CastTo<TOut>(this object self, TOut defaultValue = default(TOut))
36  {
37  TOut result;
38  //self.TryCast(typeTo: typeTo, out result, defaultValue, throwException: false);
39 
40  self.TryCast<object, TOut>(out result, defaultValue, throwException: defaultValue?.IsDefaultValue() == true, unboxObjectType: true);
41  return result;
42  }
43 
44  [MethodImpl(MethodImplOptions.AggressiveInlining)]
45  public static TOut CastTo<TOut>(this object self)
46  {
47  TOut result;
48  self.TryCast<object, TOut>(out result, default(TOut), throwException: false, unboxObjectType: true);
49  return result;
50  }
51 
75  [MethodImpl(MethodImplOptions.AggressiveInlining)]
76  public static TOut CastTo<TIn, TOut>(this TIn? self, TOut defaultValue = default(TOut), bool withContext = false) where TIn : struct
77  {
78  TOut result;
79  self.TryCast<object, TOut>(out result, defaultValue: default(TOut), throwException: false, unboxObjectType: false, contextInstance: (withContext ? new ConvertContext(default(TOut)) { Nullable = true} : null));
80  return result;
81  }
82 
108  public static TOut CastTo<TIn, TOut>(this TIn self, TOut defaultValue = default(TOut))
109  {
110  TOut result;
111 
112  // check for generic type
113  if(ConverterCollection.Initialized == true
114  && ConverterCollection.CurrentInstance?.Settings.AllowGenericTypes == false
115  && typeof(TIn).IsConstructedGenericType == true)
116  {
117  throw new ConverterException(ConverterCause.ConverterArgumentGenericType);
118  }
119 
120  self.TryCast<TIn, TOut>(out result, defaultValue, throwException: true);
121  return result;
122  }
123 
150  [MethodImpl(MethodImplOptions.AggressiveInlining)]
151  public static object CastTo(this object self, Type typeTo, object defaultValue = null, bool unboxObjectType = true)
152  {
153  object result;
154  self.TryCast(typeTo, out result, defaultValue, throwException: false, unboxObjectType: unboxObjectType);
155  return result;
156  }
157 
191  public static bool TryCast<TIn, TOut>(this TIn self, out TOut result, TOut defaultValue = default(TOut), bool throwException = false, bool unboxObjectType = false, IConvertContext contextInstance = null)
192  {
193  Converter converter;
194  var typeArgument = (defaultValue == null || defaultValue.IsDefaultValue() == true) ? null : typeof(TOut);
195  if(GetConverterOrDefault(self, out converter, out result, typeArgument: typeArgument, throwException: throwException, unboxObjectType: unboxObjectType))
196  {
197  return true;
198  }
199 
200  return InvokeConvert(self, out result, defaultValue, throwException, converter, contextInstance: contextInstance);
201  }
202 
224  public static bool TryCast(this object self, Type typeTo, out object result, object defaultValue, bool throwException = false, bool unboxObjectType = true, IConvertContext contextInstance = null)
225  {
226  Type typeFrom = null;
227  Type typeArgument = null;
228  if(unboxObjectType == true)
229  {
230  typeFrom = self.GetType();
231  if(typeFrom == typeTo)
232  {
233  result = (object)self;
234  return true;
235  }
236 
237  typeArgument = defaultValue == null ? null : defaultValue.GetType();
238  }
239 
240  Converter converter;
241  if(GetConverterOrDefault(self, out converter, out result, typeArgument: typeArgument, typeTo: typeTo, throwException: throwException, unboxObjectType: true))
242  {
243  return true;
244  }
245 
246  if(defaultValue == null)
247  {
248  defaultValue = typeTo.GetTypeInfo().GetDefault();
249  }
250 
251  return InvokeConvert(self, out result, defaultValue, throwException, converter, contextInstance: contextInstance);
252  }
253  }
254 }
ConverterCause
Contains the reasons for a ConverterException to be raised.